package com.atguigu.chapter05.source;

import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;
import org.apache.flink.streaming.util.serialization.JSONKeyValueDeserializationSchema;

import java.util.Properties;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/6/9 10:37
 */
public class Flink03_Source_Kafka {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        
        Properties props = new Properties();
        props.setProperty("bootstrap.servers", "hadoop162:9092,hadoop163:9092,hadoop164:9092");
        props.setProperty("group.id", "Flink03_Source_Kafka");
        props.setProperty("auto.reset.offset", "latest");
        //        DataStreamSource<String> s1 = env.addSource(new FlinkKafkaConsumer<String>("water", new SimpleStringSchema(), props));
        SingleOutputStreamOperator<JsonNode> s1 = env
            .addSource(new FlinkKafkaConsumer<>(
                "water",
                new JSONKeyValueDeserializationSchema(false),  // 是否包含元数据
                props))
            .map(node -> node.get("value"));
    
        s1.print();
        
        env.execute();
    }
}
